package com.jml.activitymq.consumer;


import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class Consumer发送邮件 {
	@Autowired
	JavaMailSender javaMailSender;

	@Value("${spring.mail.username}")
	private String toEmail;

	@JmsListener(destination = "${queue}")
	public void receive(String msg) throws Exception {
		System.out.println("消费者接受，生产者内容:" + msg);
		if (StringUtils.isEmpty(msg)) {
			return;
		}
		JSONObject jsonObject = JSONObject.parseObject(msg);
		String userName = jsonObject.getString("userName");
		String eamil = jsonObject.getString("eamil");
		sendSimpleMail(eamil, userName);

	}

	public void sendSimpleMail(String eamil, String userName) throws Exception {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(eamil);
		message.setTo(toEmail);
		message.setSubject("邮件主题");
		message.setText("邮件内容"+userName);
		javaMailSender.send(message);
		System.out.println("邮件发送完成," + JSONObject.toJSONString(message));
	}
}
